var gridObj;
$(function () {
  gridObj = $.fn.bsgrid.init('reportTable', {
      url: '/staff/queryTeamUserReport',
      pageSize: 10,
      pageSizeSelect:true,
      pageSizeForGrid:[5,10,15,20],
      displayBlankRows:false,
      stripeRows:true,
      rowHoverColor:true,
      lineWrap:true,
      longLengthAotoSubAndTip:false,

  });

    // 表格的checkbox选择
    if($('#reportTable thead tr th:eq(0) input[type=checkbox]').length == 1) {
      $('#reportTable thead tr th:eq(0) input[type=checkbox]').change(function () {
          var checked = $.bsgrid.adaptAttrOrProp($(this), 'checked') ? true : false;
          $.bsgrid.adaptAttrOrProp($('#reportTable tbody tr td:nth-child(1)>input[type=checkbox]'), 'checked', checked);
      });
    }

});


function checkbox(record, rowIndex, colIndex, options) {
   return '<input type="checkbox" value="' + gridObj.getColumnValue(rowIndex, gridObj.getColumnModel(colIndex).index) + '"/>';
}

//为未通过的状态增加红色
function mark(record, rowIndex, colIndex, options){
    var status =  gridObj.getRecordIndexValue(record, 'status');
    if(status == '未通过'){
        return '<span style="color:#ff0000;">'+ status +'</span>';
    }else{
        return status;
    }
}

var verify = {
  init: function() {
      var self = this;
      $('#startDate,#endDate').datetimepicker({
          format: 'yyyy-mm-dd',
          startView: 2,
          minView: 2,
          autoclose: true,
          language: 'zh-CN'
      });
      self.getUserList();
      self.getProjectList();
      self.getReportStatusList();
      self.addEvent();
  },

  // 绑定事件
  addEvent: function() {
    var self = this;
    //一键删除删除
    $('#pass').click(function(){
        self.changeReportStatus('已通过');
    });
    //一键通过
    $('#refuse').click(function(){
        self.changeReportStatus('未通过');
    });
    //搜索
    $('#search').click(function(){
      self.search();
    });
  },

  //获取直接下属
  getUserList:function(){
    $.ajax({
      url:'/staff/queryTeamUser',
      type:'POST',
      success:function(data){
        $.each(data, function (index, value) {
          $('#teamUser').append('<option value="' + value.id + '">' + value.name + '</option>');
        });
      },
      error:function(data){
      }
    });
  },

    //获取项目列表
  getProjectList:function(){
    $.ajax({
      url:'/staff/queryProject',
      type:'POST',
      success:function(data){
        $.each(data, function (index, value) {
          $('#pid').append('<option value="' + value.id + '">' + value.name + '</option>');
        });
      },
      error:function(data){
      }
    });
  },

    //获取日报类型
  getReportStatusList:function(){
    $.ajax({
      url:'/staff/queryReportStatus',
      type:'POST',
      data:{pid:0},
      success:function(data){
        $.each(data, function (index, value) {
          $('#reportStatus').append('<option value="' + value.id + '">' + value.name + '</option>');
        });
      },
      error:function(data){
      }
    });
  },

  //审核
  changeReportStatus:function(status){
    var records = verify.getCheckedRecords();
      var ids = '0';
      for(var i = 0; i < records.length; i++) {
        ids += ',' + gridObj.getRecordIndexValue(records[i], 'id');
      }
      if(ids.length < 3){
        block.showMsg('请选择至少一条记录！');
      }else{
        $.ajax({
          url:'/staff/changeReportStatus',
          type:'POST',
          data:{status:status,ids:ids},

          success:function(data){
            gridObj.refreshPage();
            $('#reportTable thead tr th:eq(0)>input').attr('checked',false);
          },
          error:function(data){
          }
        });
      }
  },
  //搜索
  search:function(){

    var params = {
      uid:$('#teamUser').val(),
      startDate:$('#startDate').val(),
      endDate:$('#endDate').val(),
      status:$('#searchStatus').val(),
      pid:$('#pid').val(),
      tdescription:$('#tdescription').val(),
      rsid:$('#reportStatus').val(),
      subordinate:$('#subordinate').val()
    };
    gridObj.search(params);
  },

  getCheckedRecords:function() {
    var records = new Array();
    $('#reportTable tbody tr').each(function() {
      if($(this).find('td:eq(0)>input:checked').length == 1){
        records[records.length] = gridObj.getRowRecord($(this));
      }
    });
    return records;
  }
};
verify.init();